<?php 
require_once('includes/class.main.php');
$manager = new MeetingManager();
require_once('./includes/class.search.php');
require_once('./includes/class.geocode.php');

include_once 'template/header.php';

?>

<!DOCTYPE html>
<html lang="en">
	<head>

	<?php MeetingManager::echoMetaData(); ?>

	<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=<? echo googleMapKey(); ?>&sensor=false" ></script>
	<script type="text/javascript" src="javascript/site.js"></script>
	<script type="text/javascript" src="javascript/mtgbldr.js"></script>
	<script type="text/javascript">

		<?php 

		$name = (isset($_GET['group'])) ? $_GET['group'] : "";
		$name = preg_replace('/"/', '\"', $name);
		$address = (isset($_GET['address'])) ? $_GET['address'] : "";
		$address = preg_replace('/"/', '\"', $address);
		$city = (isset($_GET['city'])) ? $_GET['city'] : "";
		$city = preg_replace('/"/', '\"', $city);
		$zip = (isset($_GET['zip'])) ? $_GET['zip'] : "";
		$zip = preg_replace('/"/', '\"', $zip);

		$url_name = (isset($_GET['group'])) ? preg_replace('/ /', '+',$_GET['group']) : "-";
		$url_address = (isset($_GET['address'])) ? preg_replace('/ /', '+', $_GET['address']) : "-";
		$url_city = (isset($_GET['city'])) ? preg_replace('/ /', '+', $_GET['city']) : "-";
		$url_zip = (isset($_GET['zip'])) ?  preg_replace('/ /', '+', $_GET['zip']) : "-";
		
		$url = "http://" . $_SERVER['SERVER_NAME'] . "/api/searchmeetings.php?group=" . $url_name 
			. "&address=" . $url_address . "&city=" . $url_city . "&zip=" . $url_zip;
		?>

	  	var builder = new MeetingListBuilder();
	  	
  		builder.defaultLatLng = new google.maps.LatLng(
								  			<?php echo MeetingSearch::DEFAULT_LAT; ?>,
								  			<?php echo MeetingSearch::DEFAULT_LNG; ?>); 
		builder.defaultAddress = new Address(
											"<?php echo MeetingSearch::DEFAULT_ADDRESS; ?>",
	  										"<?php echo MeetingSearch::DEFAULT_CITY; ?>",
	  		  								"<?php echo MeetingSearch::DEFAULT_ZIP; ?>");				 		  		  	
  	    builder.queryURL = "<?php echo $url; ?>";
  		builder.defaultMarkerIcon = "images/marker_star.png";

  	    builder.searchLocation.address.address = "<?php echo $address; ?>";
  		builder.searchLocation.address.city = "<?php echo $city; ?>";
  		builder.searchLocation.address.zip = "<?php echo $zip; ?>";
  		builder.searchLocation.markerIcon = "images/marker_star.png";
  	    
		builder.mapElem = "mapContent";
		builder.resultsElem = "meetingList";

		builder.displayResults = function() {
			this.clearMarkers();
			this.locations = [];

			if (this.model.length > 0) { 
				this.setMeetingDetail(this.model[0]);
			}		
			
			this.displayMeetingTable();
			this.displayMarkers();

			this.displayInfoWindow(this.searchLocation);
		};

		builder.setMeetingDetail = function(meeting) {

	  		$("#name").text(meeting.name);
			$("#meetingDay").text(meeting.day);
			$("#meetingTime").text(meeting.time);
	  		
	  		var address;
			if (meeting.location) { 
				address = meeting.location + "<br>";
			}
			address += meeting.address1;
			if (meeting.address2) { 
				address += "<br>" + meeting.address2;
			}	  	
			$("#address").html(address);

			var city = meeting.city; 
			if (city) {
				city += ", GA";
			}
			$("#city").text(city);
			$("#zip").text(meeting.zip);
			$("#locationNotes").text(meeting.locationNotes);
			$("#groupNotes").text(meeting.groupNotes);
			$("#meetingCodes").text(meeting.codes);
			
		};

  		builder.displayMeetingTable = function() { 
  			var table = "<table class=\"results\" id=\"resultsTable\">";
  	  		
  			var rowCount = 1;
  			
  			for (i = 0; i < this.model.length; ++i) { 

  	  			if (i == 0) { 
					table += "<tr class=\"results resultsHeader\">";
					table += "<td class=\"results headerCol\" colspan=\"5\">";
					table += this.model[i].name + " MEETINGS"; 
					table += "</td></tr>";	
  	  	  		}
  	  				
  				var meeting = this.model[i];
  				table += "<tr class=\"results ";

				var style;	
  				if (rowCount % 2) {
  					style = "oddRow";
  				} else {
  					style = "evenRow";
  				}

  				table += style + "\">"; 
  				
  				table += "<td class=\"results dayCol\">"
  					+ meeting.day + "</td>";
  				table += "<td class=\"results timeCol\">"
  	  				+ meeting.time + "</td>";
  				table += "<td class=\"results groupNotesCol\">"
  	  				+ meeting.groupNotes + "</td>";
  	  			table += "<td class=\"results locationNotesCol\">"
  	  				+ meeting.locationNotes + "</td>";
  		  		table += "<td class=\"results codesCol\" >"
  					+ meeting.codes + "</td>";
  	  			
  				table += "</tr>"

  				++this.searchLocation.meetingCount;
  				++rowCount;
  				if (i == 0) { 
  	  				this.searchLocation.location = meeting.location;
  				}
  			}
  			
  			$("#" + this.resultsElem).html(table);
  		};

		builder.onMeetingClick = function(idx) {
	//		this.setMeetingDetail(this.model[idx]);
		};
		
	  	$(document).ready(function() {
			builder.buildMeetingList();
	  	});

	</script>
	
	</head>

  	<body>
  	<div class="content">
  	
  	<?php include_once 'template/menu.php'; ?>
		<br style="clear: both" >
  		
		<div id="mapContent" style="width: 700px; height: 400px;  margin-left:auto; margin-right:auto; margin-top:10px; margin-bottom:10px">
		</div>
			
		<div id="meetingList" style="float:left; width: 760px; margin-right: auto; margin-left:auto;">
		
		</div>
	
  	
	<? include('template/footer.php'); ?>
		
	</div>


	</body>
</html>
  